<template>
	<view class="work-container">
		<!-- 轮播图 -->
		<uni-swiper-dot class="uni-swiper-dot-box" :info="data" :current="current" field="content">
			<swiper class="swiper-box" :current="swiperDotIndex" @change="changeSwiper">
				<swiper-item v-for="(item, index) in noticeList" :key="index">
					<view class="swiper-item" @click="clickBannerItem(item)">
						<image :src="item.imgs" mode="aspectFill" :draggable="false" />
					</view>
				</swiper-item>
			</swiper>
		</uni-swiper-dot>

		<!-- 宫格组件 -->
		<uni-section title="资产" type="line"></uni-section>
		<view class="grid-body">
			<uni-grid :column="4" :showBorder="false" @change="changeGrid1">
				<uni-grid-item v-for="(item, i) in grid1" :key="i" :index="i">
					<view class="grid-item-box">
						<uni-icons :type="item.icon" size="30"></uni-icons>
						<text class="text">{{item.label}}</text>
					</view>
				</uni-grid-item>
				<uni-grid-item v-if="showScan">
					<view class="grid-item-box">
						<uni-icons type="scan" size="30"></uni-icons>
						<text class="text">扫一扫</text>
					</view>
				</uni-grid-item>
			</uni-grid>
		</view>

		<!-- 宫格组件 -->
		<uni-section title="工单" type="line"></uni-section>
		<view class="grid-body">
			<uni-grid :column="4" :showBorder="false" @change="changeGrid2">
				<uni-grid-item v-for="(item, i) in grid2" :key="i" :index="i">
					<view class="grid-item-box">
						<uni-icons :type="item.icon" size="30"></uni-icons>
						<text class="text">{{item.label}}</text>
					</view>
				</uni-grid-item>
			</uni-grid>
		</view>
		
	<!-- 宫格组件 -->
		<uni-section title="工作任务" type="line"></uni-section>
		<view class="grid-body">
			<uni-grid :column="4" :showBorder="false" @change="changeGrid3">
				<uni-grid-item v-for="(item, i) in grid3" :key="i" :index="i">
					<view class="grid-item-box">
						<uni-icons :type="item.icon" size="30"></uni-icons>
						<text class="text">{{item.label}}</text>
					</view>
				</uni-grid-item>
			</uni-grid>
		</view>
	</view>
</template>

<script>
	import { checkPermi } from "@/utils/permission.js"
	import { listNotice } from "@/api/sys.js"
	import { qrInfo} from '@/api/base.js'
	import config from '@/config'
	const baseUrl = config.baseUrl
	export default {
		data() {
			return {
				current: 0,
				swiperDotIndex: 0,
				showScan: true,
				grid1: [{
						label: '我的资产',
						icon: 'star-filled',
						url: '/pages/asset/my'
					},
					{
						label: '资产查询',
						icon: 'search',
						url: '/pages/asset/query'
					},
					{
						label: '文档查询',
						icon: 'wallet-filled',
						url: '/pages/document/query'
					}
				],
				grid2: [{
						label: '我的工单',
						icon: 'mail-open',
						url: '/pages/flow/my'
					},
					{
						label: '审核工单',
						icon: 'mail-open-filled',
						url: '/pages/flow/check'
					},
					{
						label: '创建工单',
						icon: 'folder-add',
						url: '/pages/flow/create'
					}
				],
				grid3: [{
						label: '采购入库',
						icon: 'cart',
						url: '/pages/inOut/purchase'
					},
					{
						label: '归还入库',
						icon: 'redo',
						url: '/pages/inOut/revert'
					},
					{
						label: '出库',
						icon: 'undo',
						url: '/pages/inOut/out'
					},
					{
						label: '盘点任务',
						icon: 'shop-filled',
						url: '/pages/stock/my'
					},
					{
						label: '维修任务',
						icon: 'gear-filled',
						url: '/pages/maintain/my'
					}
				],
				noticeList: [],
				data: [{
						image: '/static/images/banner/banner01.jpg'
					},
					{
						image: '/static/images/banner/banner02.jpg'
					},
					{
						image: '/static/images/banner/banner03.jpg'
					}
				]
			}
		},
		onLoad() {
			// #ifdef WEB || H5
			this.showScan = false;
			// #endif
			
			listNotice({
				pageSize: 5,
				pageNum: 1,
				orderByColumn: 'notice_id',
				isAsc: 'DESC',
			}).then(response=>{
				for(let i=0;i<response.rows.length;i++){
					let notice = response.rows[i];
					if(!notice.imgs){
						notice.imgs = '/static/images/banner/banner03.jpg';
					}else{
						let imgs = notice.imgs.split(',');
						notice.imgs = baseUrl + [0];
					}
					this.noticeList.push(notice);
				}
			})
		},
		methods: {
			clickBannerItem(item) {
				this.$tab.navigateTo(`/pages/common/textview/index?title=${item.noticeTitle}&content=${item.noticeContent}`)
			},
			changeSwiper(e) {
				this.current = e.detail.current
			},
			changeGrid1(e) {
				if(e.detail.index >= this.grid1.length){
					this.doScan();
				}else{
					let url = this.grid1[e.detail.index].url
					this.$tab.navigateTo(url);
				}				
			},
			changeGrid2(e) {
				console.log(e)
				let url = this.grid2[e.detail.index].url
				this.$tab.navigateTo(url);
			},
			changeGrid3(e) {
				console.log(e)
				let url = this.grid3[e.detail.index].url
				this.$tab.navigateTo(url);
			},
			doScan() {
				// 允许从相机和相册扫码
				uni.scanCode({
					success: function (res) {
						let num = res.result;
						if(num.toLocaleLowerCase().startsWith("http://") || num.toLocaleLowerCase().startsWith("https://")){
							this.$modal.confirm('扫码结果是个网址，确认复制到剪切板？').then(()=>{
								uni.setClipboardData({
									data: num,
									success: function () {
										console.log('copy url success');
									}
								});
							});
							return;
						}
						qrInfo({type, num}).then(response=>{
							if(response.data == null){
								this.$modal.msgError('未查询到资产信息');								
								return;
							}
							this.$tab.navigateTo('/pages/asset/scan?id='+res.data.id);								
						}).catch(err=>{
							this.$modal.msgError('查询扫描结果失败:'+err.msg);	
						})									
					}
				});
			}
			
		}
	}
</script>

<style lang="scss">
	/* #ifndef APP-NVUE */
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #fff;
		min-height: 100%;
		height: auto;
	}

	view {
		font-size: 14px;
		line-height: inherit;
	}

	/* #endif */

	.text {
		text-align: center;
		font-size: 26rpx;
		margin-top: 10rpx;
	}

	.grid-item-box {
		flex: 1;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 15px 0;
	}

	.uni-margin-wrap {
		width: 690rpx;
		width: 100%;
		;
	}

	.swiper {
		height: 300rpx;
	}

	.swiper-box {
		height: 150px;
	}

	.swiper-item {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: center;
		align-items: center;
		color: #fff;
		height: 300rpx;
		line-height: 300rpx;
	}

	@media screen and (min-width: 500px) {
		.uni-swiper-dot-box {
			width: 400px;
			/* #ifndef APP-NVUE */
			margin: 0 auto;
			/* #endif */
			margin-top: 8px;
		}

		.image {
			width: 100%;
		}
	}
</style>